// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html


interface ad_interface {
    show_RewardVideoAd_code(code: string, succ: Function, fail: Function);
    show_RewardVideoAd(succ: Function, fail: Function);
    reward_video_code(code: string, succ: Function, fail: Function);
    show_BannerAd();
    close_BannerAd();
    show_InterAd();
    GameShare(fun: Function);
    StartRecordScreen();
    StopRecordScreen();
    ShareRecordScreen(succ: Function);
    show_my_ad();
    close_my_ad();
}


const { ccclass, property } = cc._decorator;

@ccclass
export default class Apk_adManager extends cc.Component implements ad_interface {

    onLoad() {
        cc["ad_manager"] = Apk_adManager.getInterface(); // 全局对象 给 JS用
        window["ad_manager"] = Apk_adManager.getInterface(); // 全局对象 给 JS用
    }


    static _interface: Apk_adManager = null;
    public static getInterface(): Apk_adManager {
        if (Apk_adManager._interface == null) {
            Apk_adManager._interface = new Apk_adManager();
            Apk_adManager._interface.initSDK();
        }
        return Apk_adManager._interface;
    }



    /****************************************************
     *                  广告调用接口
     ****************************************************/
    public show_RewardVideoAd_code(code: string, succ: Function, fail: Function = null) {
        this.reward_video_code(code,
            () => {
                succ && succ();
            },
            () => { //---------- 失败 事件
                fail && fail();
            });
    }
    //#################################激励视频
    public show_RewardVideoAd(succ: Function, fail: Function = null) {
        this.reward_video(
            () => {
                succ && succ();
            },
            () => { //---------- 失败 事件
                fail && fail();
            });
    }

    //################################### BANNER
    _is_banner_showing = false;
    show_BannerAd() {
        if (this._is_banner_showing == true) {
            return;
        }
        this._is_banner_showing = true;
        this.show_banner();
    }
    close_BannerAd() {
        if (this._is_banner_showing == false) {
            return;
        }
        this._is_banner_showing = false;
        this.hide_banner();
    }

    //##############################  插屏
    show_InterAd() {
        this.fullvideo();
    }

    // ############################# 游戏分享
    GameShare(fun: Function) {

    }

    // ############################# 录屏分享 (头条专属)
    StartRecordScreen() {

    }

    //停止录屏
    StopRecordScreen() {

    }

    //分享录制的视频
    ShareRecordScreen(succ: Function) {

    }

    show_my_ad() {

    }
    close_my_ad() {

    }

    /************************************************************
 *              
 ************************************************************/
    private reward_succ: Function = null;
    private reward_fail: Function = null;
    public reward_video(succ: Function, fail: Function = null) {
        this.reward_succ = succ;
        this.reward_fail = fail;

        this.callJava("show_reward");
    }

    public reward_video_code(code: string, succ: Function, fail: Function = null) {
        this.reward_succ = succ;
        this.reward_fail = fail;

        this.callJava_par("show_reward", code);
    }

    public reward_callback(succ: boolean) {
        if (succ) {
            this.reward_succ && this.reward_succ(succ);
            this.reward_succ = null;
        } else {
            this.reward_fail && this.reward_fail();
            this.reward_fail = null;
        }
    }

    public fullvideo() {
        this.callJava("show_fullvideo");
    }

    public show_banner() {
        this.callJava("show_banner");
    }

    public hide_banner() {
        this.callJava("hide_banner");
    }

    /*************************************************************
     * 
     *************************************************************/
    jsb = null;
    initSDK() {
        if (this.is_apk() == false) return;

        this.jsb = window["jsb"]
    }

    private is_apk(): boolean {
        if (cc.sys.platform == cc.sys.ANDROID && window["jsb"]) {
            return true;
        } else {
            return false;
        }
    }

    private callJava(fun_name) {
        if (this.is_apk() == false) {
            console.log("callJava -------- " + fun_name);
            return;
        }

        this.jsb.reflection.callStaticMethod("org.cocos2dx.javascript/Ad_csj", fun_name, "()V");
    }

    private callJava_par(fun_name, par) {
        if (this.is_apk() == false) {
            console.log("callJava -------- " + fun_name + " ------ " + par);
            return;
        }
        this.jsb.reflection.callStaticMethod("org.cocos2dx.javascript/Ad_csj", fun_name, "(Ljava/lang/String;)V", par);
    }



}
